function formatTimeTwo(number, format) {

  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  var returnArr = [];

  var date = new Date(number);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));

  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));

  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function isRegister(){
  // 判断用户是否登录
  let reg = wx.getStorageSync('isRegistered')
  if(reg < 1) {
    wx.redirectTo({
      url: '/pages/my/login/index',
    })
    return
  }
}

const PosterCanvas = (url,data, successFn) =>{
  const ctx = wx.createCanvasContext('myCanvas');
  ctx.clearRect(0, 0, 0, 0);
  wx.getImageInfo({
    src: url,
    success: function (res) {
      const WIDTH = res.width;
      const HEIGHT = res.height;
      ctx.drawImage(res.path, 0, 0, 320, 416);
      ctx.setFontSize(8)
      ctx.fillText(data.name, 150, 170)
      ctx.save()
      ctx.restore();
      ctx.draw(true, function () {
          wx.canvasToTempFilePath({
            canvasId: 'myCanvas',
            fileType: 'png',
            destWidth: WIDTH,
            destHeight: HEIGHT,
            success: function (res) { 
              successFn && successFn(res.tempFilePath);
            },
            fail: function (res) {
              console.log(res);
            }
          })

      });
    },
    fail: function () {
      wx.hideLoading();
      console.log('无法获取图片信息');
    }
  })
}

module.exports = {
  formatTimeTwo,
  PosterCanvas: PosterCanvas,
  isRegister
}